﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Security.Cryptography;

namespace GetMd5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = GetMD5(GetNetworkMAC());
        }

        private string GetNetworkMAC()
        {
            string strMac = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    strMac += mo["MacAddress"].ToString();

            }
            return strMac.ToUpper();
        }

        private string GetMD5(string source)
        {
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(source));
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X");

            }
            return pwd;
        }
    }
}
